﻿/******************************************************************************
    myBoard is a port of MacOS X dashboard application for windows.
    Copyright (C) 2007,2008  by Yann ALET
    Email : yann.alet@gmail.com

    myBoard is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    myBoard is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
****************************************************************************/

using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;

namespace Guide
{
    public partial class Loader
    {
        private Storyboard _waitIntroStoryboard;
        private Storyboard _waitOutroStoryboard;
        private Storyboard _rollingStoryboard;
        private TransformGroup _orignalTG;
        private int _percentProgress;
        private Canvas _templateRoot;
        private Storyboard _percent12Storyboard;
        private Storyboard _percent25Storyboard;
        private Storyboard _percent37Storyboard;
        private Storyboard _percent50Storyboard;
        private Storyboard _percent62Storyboard;
        private Storyboard _percent75Storyboard;
        private Storyboard _percent87Storyboard;
        private Storyboard _percent100Storyboard;
        bool _over12Played;
        bool _over25Played;
        bool _over37Played;
        bool _over50Played;
        bool _over62Played;
        bool _over75Played;
        bool _over87Played;
        bool _over100Played;

        public int PercentProgress
        {
            get { return _percentProgress; }
            set
            {
                _percentProgress = value;
                if (value >= 12 && !_over12Played)
                {
                    _templateRoot.BeginStoryboard(_percent12Storyboard);
                    _over12Played = true;
                }
                if (value >= 25 && !_over25Played)
                {
                    _templateRoot.BeginStoryboard(_percent25Storyboard);
                    _over25Played = true;
                }
                if (value >= 37 && !_over37Played)
                {
                    _templateRoot.BeginStoryboard(_percent37Storyboard);
                    _over37Played = true;
                }
                if (value >= 50 && !_over50Played)
                {
                    _templateRoot.BeginStoryboard(_percent50Storyboard);
                    _over50Played = true;
                }
                if (value >= 62 && !_over62Played)
                {
                    _templateRoot.BeginStoryboard(_percent62Storyboard);
                    _over62Played = true;
                }
                if (value >= 75 && !_over75Played)
                {
                    _templateRoot.BeginStoryboard(_percent75Storyboard);
                    _over75Played = true;
                }
                if (value >= 87 && !_over87Played)
                {
                    _templateRoot.BeginStoryboard(_percent87Storyboard);
                    _over87Played = true;
                }
                if (value == 100 && !_over100Played)
                {
                    _templateRoot.BeginStoryboard(_percent100Storyboard);
                    _over100Played = true;
                }
            }
        }


        public Loader()
        {
            this.InitializeComponent();
            _waitIntroStoryboard = this.FindResource("WaitIntro") as Storyboard;
            _waitOutroStoryboard = this.FindResource("WaitOutro") as Storyboard;
            _rollingStoryboard = this.FindResource("Rolling") as Storyboard;
            _waitIntroStoryboard.Completed += new EventHandler(_waitIntroStoryboard_Completed);
            _waitOutroStoryboard.Completed += new EventHandler(_waitOutroStoryboard_Completed);
            _orignalTG = new TransformGroup();
            ScaleTransform st = new ScaleTransform(1, 1);
            SkewTransform skt = new SkewTransform(0, 0);
            RotateTransform rt = new RotateTransform(0);
            TranslateTransform tt = new TranslateTransform();
            _orignalTG.Children.Add(st);
            _orignalTG.Children.Add(skt);
            _orignalTG.Children.Add(rt);
            _orignalTG.Children.Add(tt);
        }

        void _waitOutroStoryboard_Completed(object sender, EventArgs e)
        {
            //_rollingStoryboard.Stop(this);
            //_waitIntroStoryboard.Stop(this);
            //_waitOutroStoryboard.Stop(this);
            //_waitLogo.RenderTransform = _orignalTG;
        }

        void TemplateRoot_Loaded(object sender, RoutedEventArgs e)
        {
            _templateRoot = sender as Canvas;
            _percent12Storyboard = _templateRoot.FindResource("percent12") as Storyboard;
            _percent25Storyboard = _templateRoot.FindResource("percent25") as Storyboard;
            _percent37Storyboard = _templateRoot.FindResource("percent37") as Storyboard;
            _percent50Storyboard = _templateRoot.FindResource("percent50") as Storyboard;
            _percent62Storyboard = _templateRoot.FindResource("percent62") as Storyboard;
            _percent75Storyboard = _templateRoot.FindResource("percent75") as Storyboard;
            _percent87Storyboard = _templateRoot.FindResource("percent87") as Storyboard;
            _percent100Storyboard = _templateRoot.FindResource("percent100") as Storyboard;
        }

        public void ShowLoader()
        {
            this.BeginStoryboard(_waitIntroStoryboard);
        }

        public void CloseLoader()
        {
            
            this.BeginStoryboard(_waitOutroStoryboard);
            
            _over12Played = false;
            _over25Played = false;
            _over37Played = false;
            _over50Played = false;
            _over62Played = false;
            _over75Played = false;
            _over87Played = false;
            _over100Played = false;
        }

        void _waitIntroStoryboard_Completed(object sender, EventArgs e)
        {
            _waitLogo.RenderTransform = _orignalTG;
            this.BeginStoryboard(_rollingStoryboard);
        }
    }
}